﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelSoft.HotelManagement_BUS;
using HotelSoft.HotelManagement_DTO;

namespace HotelSoft.HotelManagement_GUI
{
    public partial class frmLogin : Form
    {
        // Define the CS_DROPSHADOW constant
        private const int CS_DROPSHADOW = 0x00020000;

        // Override the CreateParams property
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }

        AccountDTO acc;
        int dem = 0;
        Double temp = 1;
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void LoginAccount()
        {
            acc=new AccountDTO();
            acc.USERNAME=txtUsername.Text;
            acc.PASSWORD=txtPassword.Text;
            int role;
            role=HotelManagement_BUS.AccountBUS.LoginAccount(acc);
            if (role == 0)
            {
                dem++;
                if (dem == 3)
                {
                    MessageBox.Show("Bạn đã nhập sai mật khẩu 3 lần, chương trình sẽ thoát");
                    Application.Exit();
                }
                MessageBox.Show("Tên Tài Khoản và mật khẩu không khớp, xin vui lòng nhập lại", "Đăng Nhập");
                txtPassword.Focus();
                txtPassword.SelectAll();
                return;
            }
            else
                if (role==1)
                {
                    frmMain_Giamdoc gd = new frmMain_Giamdoc();
                    this.Hide();
                    gd.Show();
                    gd.FormClosing += new FormClosingEventHandler(gd_FormClosing);
                    return ;
                }
                else
                    if (role==2)
                    {
                        frmMain_Quanly ql = new frmMain_Quanly();
                        this.Hide();
                        ql.Show();
                        ql.FormClosing += new FormClosingEventHandler(ql_FormClosing);
                        return;
                    }
                    else
                        if(role==3)
                        {
                            frmMain_Tieptan tt = new frmMain_Tieptan();
                            this.Hide();
                            tt.Show();
                            tt.FormClosing += new FormClosingEventHandler(tt_FormClosing);
                            return;
                        }
                        else
                        {
                            frmQuanly_Account_Quantri qt = new frmQuanly_Account_Quantri();
                            this.Hide();
                            qt.Show();
                            qt.FormClosing += new FormClosingEventHandler(qt_FormClosing);
                            return;
                        }
                
        }

        void qt_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Show();
            txtPassword.Text = "";
            txtPassword.Focus();
            dem = 0;
        }

        void tt_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Show();
            txtPassword.Text = "";
            txtPassword.Focus();
            dem = 0;
        }

        void ql_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Show();
            txtPassword.Text = "";
            txtPassword.Focus();
            dem = 0;
        }

        void gd_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Show();
            txtPassword.Text = "";
            txtPassword.Focus();
            dem = 0;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            SqlDataBUS busSQL = new SqlDataBUS();
            SqlDataDTO dtoSQL = new SqlDataDTO();
            if (!busSQL.doLogin())
            {
                frmConnection frm = new frmConnection();
                this.Hide();
                frm.Show();
                frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
            }
            else
            {
                if (txtUsername.Text == "" || txtUsername.Text.Trim() == "" || txtPassword.Text == "" || txtPassword.Text.Trim() == "")
                {
                    MessageBox.Show("Xin hãy nhập đầy đủ thông tin !", "ĐĂNG NHẬP");
                    txtUsername.Focus();
                    return;
                }
                LoginAccount();
            }
        }

        void frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Show();
            txtUsername.Focus();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
            {
                timer1.Enabled = false;
            }
        }

        private void lLblChangePass_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmChangePassword ch = new frmChangePassword();
            this.Hide();
            ch.Show();
            ch.FormClosing += new FormClosingEventHandler(ch_FormClosing);
        }

        void ch_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Show();
            txtPassword.Text = "";
            txtPassword.Focus();
            dem = 0;
        }

        private void txtPassword_Enter(object sender, EventArgs e)
        {
            txtPassword.SelectAll();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
